package com.pan.basic;


import sun.misc.Unsafe;

import java.lang.reflect.Field;

class Car {

    int intParam = 3;

    long longParam =5L;

    String strParam;
}
public class UnsafeTest {

    private static Unsafe unsafe;

    static{
        try {
            Field field = Unsafe.class.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            unsafe = (Unsafe)field.get(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {

        Class clazz = Car.class;
        //获取所有声明字段
        Field[] fields = clazz.getDeclaredFields();
        System.out.println("开始测试偏移量。");
        //一个实例存在于内存，不同的属性占用不同的内存空间，偏移量就是这个属性所在空间的内存地址
        for(Field f : fields){
            //获取属性偏移量，可以通过这个偏移量给属性设值
            System.out.println(f.getName()+" : "+unsafe.objectFieldOffset(f));
        }
        System.out.println("--------------------------------");
        Car car = new Car();
        Field intParam = clazz.getDeclaredField("intParam");
        int a = (Integer) intParam.get(car);
        System.out.println("intParam原始值："+a);

        long intParamOffset = unsafe.objectFieldOffset(intParam);
        System.out.println("intParam实例变量修改前偏移量："+intParamOffset);
        //intParam实例变量偏移量是offset 原始值是3，我们要改成10
        System.out.println(unsafe.compareAndSwapInt(car, intParamOffset, 3, 10));
        System.out.println("intParam改变之后的值："+car.intParam);

        long intParamOffsetAfter = unsafe.objectFieldOffset(intParam);
        System.out.println("intParam改变之后偏移量是："+intParamOffsetAfter);
        System.out.println("------------改为10后，再次调用,返回false--------------------");
        System.out.println(unsafe.compareAndSwapInt(car, intParamOffset, 3, 10));

        System.out.println("------------开始修改long--------------------");
        Field longParam = clazz.getDeclaredField("longParam");
        long longInit = (Long) longParam.get(car);
        System.out.println("longParam原始值："+longInit);
        long longParamOffset = unsafe.objectFieldOffset(longParam);
        System.out.println("longParam实例变量的偏移量是："+longParamOffset);
        System.out.println(unsafe.compareAndSwapLong(car, longParamOffset, 5L, 111L));
        System.out.println("longParam改变之后的值："+car.longParam);

        System.out.println("------------开始修改string--------------------");
        Field strParam = clazz.getDeclaredField("strParam");
        String str = (String) strParam.get(car);
        System.out.println("strParam原始值："+str);
        long strParamOffset = unsafe.objectFieldOffset(strParam);
        System.out.println("strParam实例变量的偏移量是："+strParamOffset);
        System.out.println(unsafe.compareAndSwapObject(car, strParamOffset, null, "5"));
        System.out.println("strParam改变之后的值："+car.strParam);

    }

}

